﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Shine.Framework;
using System.Runtime.InteropServices;
using SuperMap.Realspace;

namespace Shine.FlyManager
{
    public partial class RouteList : UserControl, ICommand
    {
        IApplication m_Application = null;
        public RouteList()
        {
            InitializeComponent();
            comboBoxEx1.Items.Clear();
            
            //string path = Application.StartupPath + "\\飞行路线";
            //if (System.IO.Directory.Exists(path) == false) return;
            //string[] files = System.IO.Directory.GetFiles(path, "*.fpf", SearchOption.AllDirectories);
            //foreach (string str in files)
            //{
            //    string name = System.IO.Path.GetFileNameWithoutExtension(str);
            //    comboBoxEx1.Items.Add(name);
            //}
        }

        #region ICommand Members

        int ICommand.Bitmap
        {
            get { return 0; }
        }

        string ICommand.Caption
        {
            get { return "RouteList"; }
        }

        string ICommand.Category
        {
            get { return "飞行路线"; }
        }

        bool ICommand.Checked
        {
            get { return false; }
        }

        bool ICommand.Enabled
        {
            get
            {
                return true;
            }
        }

        int ICommand.HelpContextID
        {
            get { return 0; }
        }

        string ICommand.HelpFile
        {
            get { return string.Empty; }
        }

        string ICommand.Message
        {
            get { return "RouteList"; }
        }

        string ICommand.Name
        {
            get { return "RouteList"; }
        }

        void ICommand.OnClick()
        {
        }

        void ICommand.OnCreate(object hook)
        {
            if (m_Application == null)
            {
                m_Application = hook as IApplication;
                (m_Application as IApplicationEvents).OnLoadFlyRouteEvent += RouteList_OnLoadFlyRouteEvent;
            }        
        }

        void RouteList_OnLoadFlyRouteEvent(object sender, SceneRouteEventArgs e)
        {
            //throw new NotImplementedException();
            this.comboBoxEx1.Items.Clear();
            Routes rts = e.CurrentRoute;
            foreach (var rt in rts)
            {
                ComboBox cbx = new ComboBox();                
                cbx.Text = (rt as Route).Name;
                cbx.Tag = rt;
                comboBoxEx1.Items.Add(cbx);
            }

            if(comboBoxEx1.Items.Count>0)
            {
                comboBoxEx1.SelectedIndex = 0;
            }
        }

        string ICommand.Tooltip
        {
            get { return "飞行路线"; }
        }

        #endregion      

        private void comboBoxEx1_KeyDown(object sender, KeyEventArgs e)
        {
            
        }

        private void comboBoxEx1_SelectedIndexChanged(object sender, EventArgs e)
        {
            (m_Application as IFramework).SceneControl.Scene.FlyManager.Stop();

            (m_Application as IFramework).SelectedRoute = comboBoxEx1.SelectedItem == null ? "" : ((ComboBox)(comboBoxEx1.SelectedItem)).Text.ToString();
            //(m_Application as IFramework).SceneControl.Scene.FlyManager.Routes.CurrentRoute = ((ComboBox)(comboBoxEx1.SelectedItem)).Tag as Route;
            int routeNum = (m_Application as IFramework).SceneControl.Scene.FlyManager.Routes.Count;
            for(int i =0;i<routeNum;i++)
            {
                if((m_Application as IFramework).SelectedRoute == (m_Application as IFramework).SceneControl.Scene.FlyManager.Routes[i].Name)
                {
                    (m_Application as IFramework).SceneControl.Scene.FlyManager.Routes.CurrentRouteIndex = i;
                    break;
                }
            }
        }
    }
}

